//
//  AppDelegate.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/3/16.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "AppDelegate.h"
#import "TabBarViewController.h"
#import "configuration.h"
#import "WYCLoginViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "SetGestureLockViewController.h"
#import "WXApi.h"
@interface AppDelegate ()<WXApiDelegate>
@property (nonatomic,strong)SetGestureLockViewController *lvc;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(InfoNotificationAction:) name:@"yes" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(InfoNotificationNO:) name:@"no" object:nil];
    
    
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //创建并初始化UITabBarController
   
    [WXApi registerApp:@"wxfef8aede6e91ab83" withDescription:@"wechat"];

    if ([WYCAccountTool unarchivetoken])
    {
        if (![[NSUserDefaults standardUserDefaults] boolForKey:@"first"]){
            self.window.rootViewController = [[TabBarViewController alloc] init];
        }else{
            self.window.rootViewController = [[TabBarViewController alloc] init];
//            SetGestureLockViewController * sg = [[SetGestureLockViewController alloc] init];
//            sg.myflag = @"666";
//            self.window.rootViewController = sg;
        }
        
    }else{
        
        self.window.rootViewController = [[WYCLoginViewController alloc] init];
        
    }
    return YES;
}

- (void)setKeyBoardManager
{
    //IQkey
    [[IQKeyboardManager sharedManager] setEnable:YES];
    [[IQKeyboardManager sharedManager] setKeyboardDistanceFromTextField:20];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:YES];
    [[IQKeyboardManager sharedManager] setToolbarManageBehaviour:IQAutoToolbarBySubviews];
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES];
}

- (void)InfoNotificationNO:(NSNotification *)notification{
//    SetGestureLockViewController * sg = [[SetGestureLockViewController alloc] init];
//    self.lvc = sg;
//    sg.myflag = @"666";
//    //self.window.rootViewController = sg;
//    sg.view.frame = CGRectMake(0, 0, kScreenW, kScreenH);
//    
//    [[UIApplication sharedApplication].keyWindow addSubview:sg.view];
    
    
    
//    self.window.rootViewController = [[SetGestureLockViewController alloc] init];
    
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    
    if ([WYCAccountTool unarchivetoken])
    {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"first"]) {
        SetGestureLockViewController * sg = [[SetGestureLockViewController alloc] init];
        self.lvc = sg;
        sg.myflag = @"666";
        //self.window.rootViewController = sg;
        sg.view.frame = CGRectMake(0, 0, kScreenW, kScreenH);
        
        [[UIApplication sharedApplication].keyWindow addSubview:sg.view];
        
        
        
       }
    }
    else
    {
        
//        self.window.rootViewController = [[WYCLoginViewController alloc] init];

    }
  
    // app启动或者app从后台进入前台都会调用这个方法
}
- (void)InfoNotificationAction:(NSNotification *)notification{
    
    //self.window.rootViewController = [[TabBarViewController alloc] init];
    [self.lvc.view removeFromSuperview];
}
//// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
    }
    return  [WXApi handleOpenURL:url delegate:self];
}
//-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
//{
//    return [WXApi handleOpenURL:url delegate:self];
//}




- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    return [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation{
    return [WXApi handleOpenURL:url delegate:self];
}

-(void) onResp:(BaseResp*)resp{
    NSLog(@"resp %d",resp.errCode);
    
    /*
     enum  WXErrCode {
     WXSuccess           = 0,    成功
     WXErrCodeCommon     = -1,  普通错误类型
     WXErrCodeUserCancel = -2,    用户点击取消并返回
     WXErrCodeSentFail   = -3,   发送失败
     WXErrCodeAuthDeny   = -4,    授权失败
     WXErrCodeUnsupport  = -5,   微信不支持
     };
     */
    if ([resp isKindOfClass:[SendAuthResp class]]) {   //授权登录的类。
        if (resp.errCode == 0) {  //成功。
            //这里处理回调的方法 。 通过代理吧对应的登录消息传送过去。
            if ([_wxDelegate respondsToSelector:@selector(loginSuccessByCode:)]) {
                SendAuthResp *resp2 = (SendAuthResp *)resp;
                [_wxDelegate loginSuccessByCode:resp2.code];
            }
        }else{ //失败
            NSLog(@"error %@",resp.errStr);
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登录失败" message:[NSString stringWithFormat:@"reason : %@",resp.errStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];
//            [alert show];
        }
    }
    
    if ([resp isKindOfClass:[SendMessageToWXResp class]]) { //微信分享 微信回应给第三方应用程序的类
        SendMessageToWXResp *response = (SendMessageToWXResp *)resp;
        NSLog(@"error code %d  error msg %@  lang %@   country %@",response.errCode,response.errStr,response.lang,response.country);
        
        if (resp.errCode == 0) {  //成功。
            //这里处理回调的方法 。 通过代理吧对应的登录消息传送过去。
            if (_wxDelegate) {
                if([_wxDelegate respondsToSelector:@selector(shareSuccessByCode:)]){
                    [_wxDelegate shareSuccessByCode:response.errCode];
                }
            }
        }else{ //失败
            NSLog(@"error %@",resp.errStr);
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"reason : %@",resp.errStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];
//            [alert show];
        }
    }
    
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}




- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
